<?php

/**
 * The DbSet class has methods update the database.
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class DbSet extends Db {

    /**
     * Add a log record of account access
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function accesslogs($data) {
        return parent::put('accesslogs', $data);
    }
    
    /**
     * Add data in accounts table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @param string $type [optional]<br>The type to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function accounts($data, $id = null, $type = null) {
        if ($id == null)
            $rs = parent::put('accounts', $data);
        else {
            if ($account = DbGet::account($id, $type)) {
                $cond = array('id' => $account->id, 'type' => $account->type);
                $rs = parent::put('accounts', $data, $cond);
            }
            else
                $rs = false;
        }
        return $rs;
    }
    
    /**
     * Add data in banners table
     * 
     * @param array $data The array with all data
     * @param int $type The type of banner
     * @param int|array $id [optional]<br>One or more identity
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function banners($data, $type, $id = null) {
        $cond = array('type' => $type);
        if ($id != null)
            $cond['id'] = $id;
        $rs = parent::put('banners', $data, $cond);
        return $rs;
    }
    
    /**
     * Add data in customers table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean|int The id of the new insert or is upgraded<br>
     *                     <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function customers($data, $id = null) {
        if ($id == null)
            $rs = (parent::put('customers', $data)) ? parent::$insert_id : false;
        else {
            if ($customer = DbGet::customer($id)) {
                $cond = array('id' => $customer->id);
                $rs = parent::put('customers', $data, $cond);
            }
            else
                $rs = false;
        }
        return $rs;
    }
    
    /**
     * Add phones of the customers
     * 
     * @param array $id_customer The identity of the customer
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function customers_phones($id_customer, $data, $id = null) {
        if ($id == null) {
            if (parent::put('phones', $data)) {
                $id_phone = parent::$insert_id;
                $data = array('id_customer' => $id_customer, 'id_phone' => $id_phone);
                $rs = parent::put('customers_phones', $data);
            }
            else
                $rs = false;
        }
        else {
            $cond = array('id' => $id);
            $rs = parent::put('phones', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add transactions of the customers
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function customers_transactions($data, $id = null) {
        if ($id == null)
            $rs = parent::put('customers_transactions', $data);
        else {
            $cond = array('id' => $id);
            $rs = parent::put('customers_transactions', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add vehicles of the customers
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function customers_vehicles($data, $id = null) {
        if ($id == null)
            $rs = (parent::put('customers_vehicles', $data)) ? parent::$insert_id : false;
        else {
            $cond = array('id' => $id);
            $rs = parent::put('customers_vehicles', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add data in enterprises table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean|int The id of the new insert or is upgraded<br>
     *                     <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function enterprises($data, $id = null) {
        if ($id == null)
            $rs = (parent::put('enterprises', $data)) ? parent::$insert_id : false;
        else {
            if ($enterprise = DbGet::enterprise($id)) {
                $cond = array('id' => $enterprise->id);
                $rs = parent::put('enterprises', $data, $cond);
            }
            else
                $rs = false;
        }
        return $rs;
    }
    
    /**
     * Add phones of the enterprises
     * 
     * @param array $id_enterprise The identity of the enterprise
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function enterprises_phones($id_enterprise, $data, $id = null) {
        if ($id == null) {
            if (parent::put('phones', $data)) {
                $id_phone = parent::$insert_id;
                $data = array('id_enterprise' => $id_enterprise, 'id_phone' => $id_phone);
                $rs = parent::put('enterprises_phones', $data);
            }
            else
                $rs = false;
        }
        else {
            $cond = array('id' => $id);
            $rs = parent::put('phones', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add transactions of the enterprises
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function enterprises_transactions($data, $id = null) {
        if ($id == null)
            $rs = parent::put('enterprises_transactions', $data);
        else {
            $cond = array('id' => $id);
            $rs = parent::put('enterprises_transactions', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add vehicles of the enterprises
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function enterprises_vehicles($data, $id = null) {
        if ($id == null)
            $rs = parent::put('enterprises_vehicles', $data);
        else {
            $cond = array('id' => $id);
            $rs = parent::put('enterprises_vehicles', $data, $cond);
        }
        return $rs;
    }
       
    /**
     * Add data in proposals table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function proposals($data, $id = null) {
        if ($id == null)
            $rs = parent::put('proposals', $data);
        else {
            $cond = array('id' => $id);
            $rs = parent::put('proposals', $data, $cond);
        }
        return $rs;
    }
    
  
    /**
     * Add data in vehicles table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean|int The id of the new insert or is upgraded<br>
     *                     <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function vehicles($data, $id = null) {
        if ($id == null) {
            if (parent::put('vehicles', $data))
                $rs = parent::$insert_id;
            else
                $rs = false;
        }
        else {
            $cond = array('id' => $id);
            $rs = parent::put('vehicles', $data, $cond);
        }
        return $rs;
    }
    
    /**
     * Add data in vehicles_details table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function vehicles_details($data) {
        return parent::put('vehicles_details', $data);
    }
    
    /**
     * Add data in vehicles_differentials table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function vehicles_differentials($data) {
        return parent::put('vehicles_differentials', $data);
    }
    
    /**
     * Add data in vehicles_pictures table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function vehicles_pictures($data) {
        return parent::put('vehicles_pictures', $data);
    }
    
    /**
     * Add data in products table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean|int The id of the new insert or is upgraded<br>
     *                     <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products($data,$id=NULL) {
        if ($id == null) {
            if (parent::put('products', $data))
                $rs = parent::$insert_id;
            else
                $rs = false;
        }
        else {
            $cond = array('id' => $id);
            $rs = parent::put('products', $data, $cond);
        }
        return $rs;
    }
    /**
     * Add data in products_owners table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products_owners($data) {
        return parent::put('products_owners', $data);
    }
    /**
     * Add data in products_pictures table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products_pictures($data) {
        return parent::put('products_pictures', $data);
    }
    /**
     * Add data in proposals table
     * 
     * @param array $data The array with all data
     * @param int $id [optional]<br>The identity to update
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products_proposals($data, $id = null) {
        if ($id == null)
            $rs = parent::put('products_proposals', $data);
        else {
            $cond = array('id' => $id);
            $rs = parent::put('products_proposals', $data, $cond);
        }
        return $rs;
    }
    
    
    /**
     * Add data in products_client_status table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products_client_status($data) {
        return parent::put('products_client_status', $data);
    }
    
    /**
     * Add data in products_client_status table
     * 
     * @param array $data The array with all data
     * @return boolean <b>TRUE</b> on success or <b>FALSE</b> on failure
     */
    public static function products_transactions($data) {
        return parent::put('products_transactions', $data);
    }

}